package com.yugu.park.business.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yugu.park.business.entity.AdmissionRecord;
import com.yugu.park.business.entity.CardInfo;
import com.yugu.park.business.entity.RechargeRecord;
import com.yugu.park.business.mapper.CardInfoMapper;
import com.yugu.park.business.mapper.MemberInfoMapper;
import com.yugu.park.business.service.RechargeRecordService;
import com.yugu.park.business.mapper.RechargeRecordMapper;
import com.yugu.park.dto.RechargeRecordDto;
import com.yugu.util.BeanCopyUtil;
import com.yugu.util.SearchPage;
import com.yugu.util.StringUtils;
import com.yugu.util.SysQueryParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

/**
* @author Noble-Six
* @description 针对表【t_recharge_record】的数据库操作Service实现
* @createDate 2024-04-25 11:02:34
*/
@Service
public class RechargeRecordServiceImpl extends ServiceImpl<RechargeRecordMapper, RechargeRecord>
    implements RechargeRecordService{
    @Resource
    private RechargeRecordMapper rechargeRecordMapper;
    @Resource
    private CardInfoMapper cardInfoMapper;
    @Resource
    private MemberInfoMapper memberInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdate(RechargeRecord record) {
        RechargeRecord old = rechargeRecordMapper.selectById(record.getId());
        if (old != null) {
            BeanCopyUtil.copyProperties(record,old);
            rechargeRecordMapper.updateById(old);
        }else {
            record.setId(IdUtil.simpleUUID());
            CardInfo cardInfo = cardInfoMapper.selectById(record.getCardId());
            if (1 == cardInfo.getStatus()){
                Date endDate = cardInfo.getEndDate();
                Instant instant = endDate.toInstant();
                ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
                LocalDate localDate = zonedDateTime.toLocalDate();
                LocalDate local = localDate.plusYears(1);
                ZonedDateTime dateTime = local.atStartOfDay(ZoneId.systemDefault());
                Date from = Date.from(dateTime.toInstant());
                cardInfo.setEndDate(from);
                cardInfoMapper.updateById(cardInfo);
            }else if
            (2 == cardInfo.getStatus()){
                Date date = new Date();
                cardInfo.setBeginDate(date);
                LocalDate beginLocalDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                LocalDate endLocalDate = beginLocalDate.plusYears(1);
                Date end = Date.from(endLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
                cardInfo.setEndDate(end);
                cardInfo.setStatus(1);
                cardInfoMapper.updateById(cardInfo);
            }
            if (StringUtils.isBlank(record.getMobile())){
                record.setMobile(memberInfoMapper.selectById(cardInfo.getMemberId()).getMobile());
            }
            rechargeRecordMapper.insert(record);
        }
    }

    @Override
    public PageInfo<RechargeRecordDto> page(SearchPage<SysQueryParam> search) {
        PageHelper.startPage(search.getPageNum(),search.getPageSize());
        List<RechargeRecordDto> list = rechargeRecordMapper.page(search.getSearch());
        return PageInfo.of(list);
    }
}




